/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.assembler;

import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.assembler.IDataMapperAssembler;
import com.inspur.edp.bff.api.manager.context.DataMapperContext;
import com.inspur.edp.bff.spi.action.datamapping.DataMappingAction;
import com.inspur.edp.bff.spi.action.datamapping.DataReversalMappingAction;
import com.inspur.edp.bff.spi.action.datamapping.DefaultDataMappingAction;
import com.inspur.edp.bff.spi.action.datamapping.DefaultDataReversalAction;
import com.inspur.edp.bff.spi.event.datamapping.BffDataMappingEventBroker;
import com.inspur.edp.cef.entity.entity.IEntityData;

import java.util.ArrayList;

public abstract class AbstractDataMapperAssembler implements IDataMapperAssembler {

    protected abstract ArrayList<DataMappingAction> getDataMappingActions(DataMapperContext mappingContext);

    protected abstract ArrayList<DataReversalMappingAction> getDataReversalMappingActions(DataMapperContext mappingContext);

    public final void doDataMappingFromVo(IFSManagerContext context, DataMapperContext mappingContext) {
        ArrayList<DataReversalMappingAction> actions = getDataReversalMappingActions(mappingContext);
        if (actions == null || actions.size() < 1) {
            actions = new ArrayList<DataReversalMappingAction>();
            DefaultDataReversalAction detaultAction = new DefaultDataReversalAction(mappingContext);
            actions.add(detaultAction);
        }

        for (DataReversalMappingAction action : actions) {
            action.deal(context);
        }
    }

    public final ArrayList<IEntityData> doDataMapping2Vo(IFSManagerContext context, DataMapperContext mappingContext) {
        ArrayList<DataMappingAction> actions = getDataMappingActions(mappingContext);
        if (actions == null || actions.size() < 1) {
            actions = new ArrayList<DataMappingAction>();
            DefaultDataMappingAction detaultAction = new DefaultDataMappingAction(mappingContext);
            actions.add(detaultAction);
        }
        try {
            BffDataMappingEventBroker.fireBeforeTotalDataMapping(mappingContext, context);
            for (DataMappingAction action : actions) {
                try {
                    BffDataMappingEventBroker.fireBeforeDataMapping(action, mappingContext, context);
                    action.deal(context);
                    BffDataMappingEventBroker.fireAfterDataMapping(mappingContext, context);
                } catch (Exception e) {
                    BffDataMappingEventBroker.fireQueryUnnormalStop(e);
                    throw e;
                }
            }
            BffDataMappingEventBroker.fireAfterTotalDataMapping(mappingContext, context, actions.size());
        } catch (Exception e) {
            BffDataMappingEventBroker.fireQueryUnnormalStop(e);
            throw e;
        }
        return mappingContext.getTargetResult();
    }
}
